﻿#function(result)

local monitor_tab =exec("""app:\scripting\library\monitor_postp\monitor_post_v1.mx""");


local result2 = clone(result);
local dparam = result2.dparam;

local check_folder=function(target_folder)
{
    switch(fs.exists(target_folder))
        {
        case 0:
            // 不存在，创建文件夹
            fs.mkdir(target_folder);
            logger.log_message("Create folder: %s", target_folder);
        case 1:
            // 错误是个文件
            logger.log_error("Cannot create folder: %s", target_folder);
            break;
        case 2:
        }
}

local file_full_path = dparam.str_macro.get("$(FULLPATH)");
local file_folder = fs.filename(file_full_path).path;

local output_path = file_folder;
local input_param_path = fs.filename(output_path.."/output/").fullpath;
local input_param_file = fs.makepath("input_param.mxd", input_param_path);
// println("input_param_file:"..input_param_file);
local input_param_data = io.load_var(input_param_file);

// io.save_var('''C:\Users\SDJ-JSJ074\Desktop\input_param_data.mxd''', input_param_data);


local probe_path = fs.filename(output_path.."/probe/").fullpath;
local plane_path = fs.filename(output_path.."/plane/").fullpath;
local cable_current_path = fs.filename(output_path.."/cable_current/").fullpath;
local cable_voltage_path = fs.filename(output_path.."/cable_voltage/").fullpath;
local lumped_port_path = fs.filename(output_path.."/lumped_port/").fullpath;

map(check_folder, [probe_path, plane_path, cable_current_path, cable_voltage_path, lumped_port_path]);

result2.dparam = nil;
result2.eparam = nil;
result2.source = nil;

result2 = table.remove_nil(result2);



local probe_data = table();
local plane_data = table();
local cable_data = table();
local lumped_port_data = table();

local count1 = 0;
for(k,v in result2)
{
    for(k1,v1 in v)
    {
        if(v1.type == "DMonitorTPoint")
        {
            //probe[0].data[0][0]
            v1.alias = input_param_data.probe[0].data[count1][0];
            probe_data[k..k1] = v1;
            count1++;
        }
        else if(v1.type == "DMonitorFSliceCpx")
        {
            v1.alias = k;
            plane_data[k..k1] = v1;
        }
        else if(v1.type == "DCableBundle")
        {
            v1.alias = k;
            cable_data[k..k1] = v1;
        }
        else if(v1.type == "DLumpedPort")
        {
            v1.alias = k;
            lumped_port_data[k..k1] = v1;   
        }
        else
        {
            throw "No object can be selected!!!";
        }
    }
}
// io.save_var('''C:\Users\SDJ-JSJ074\Desktop\sum_info.mxd''', lumped_port_data);

local sum_info = table();
local temp = table();
temp.pre_design_name = "HEMP_design";
temp.sim_scene = 3;
temp.name = "antenna_coupling_project";
temp.geometry_file_name = "";

sum_info.sim_project_info = temp;

sum_info.result_info = [];


// sum_info.probe = table();
// sum_info.plane = table();
// sum_info.cable = table();
// sum_info.lumped_port = table();

local count = 1;
for(k,v in probe_data)
{
    local name = v.alias;
    local info = monitor_tab.deal_probe_monitor(v, probe_path, dparam, name);
    sum_info.result_info.push(info);
    count++;
}

count=1;
for(k,v in plane_data)
{
    local name = v.alias.."_"..count;
    local info = monitor_tab.deal_plane_monitor(v, plane_path, dparam, name);
    sum_info.result_info.push(info);
    count++;
}

count=1;
for(k,v in cable_data)
{
    local name = v.alias.."_"..count;
    local info1 = monitor_tab.deal_cable_current_monitor(v, cable_current_path, dparam, name);
    local info2 = monitor_tab.deal_cable_voltage_monitor(v, cable_voltage_path, dparam, name);

    sum_info.result_info.push(info1);
    sum_info.result_info.push(info2);

    count++;
}

count=1;
for(k,v in lumped_port_data)
{
    local name = v.alias.."_"..count.."_Voltage";
    local info = monitor_tab.deal_lumped_port(v, lumped_port_path, dparam, name);
    sum_info.result_info.push(info);
    count++;
}

local p = io.to_json(sum_info);
io.save_txt(output_path.."/result_temple.json", p);


//输出全局json路径
mpath= os.getenv("HOMEDRIVE")..os.getenv("HOMEPATH");
local input_dir = fs.makepath("PostProcessingJson",mpath);
check_folder(input_dir);
local path_and_file = input_dir.."/HEMP.json";
if(fs.exists(path_and_file))
{

    local input_dir_json=io.handler.json.load(path_and_file);
    input_dir_json.inputDir.push(file_folder)

    local p = io.to_json(input_dir_json);     //注意json保存名称和位置
    io.save_txt(path_and_file, p);   
}
else
{

    local input_json = table();
    input_json.inputDir = [];
    input_json.inputDir.push(file_folder);
    local p = io.to_json(input_json);     //注意json保存名称和位置
    io.save_txt(path_and_file, p);    
}


// println("aaaaaaaaaaaaaaaaaaaaaaaa")
// local test=table();
// test.sum_info = sum_info;
// test.path = output_path.."general_info.json";
